package net.bxx2004.cp.commands.subs;

import net.bxx2004.cp.CloudPlugin;
import net.bxx2004.pandalib.pcommands.PSubcommands;
import net.bxx2004.pandalib.planguage.PMessage;
import net.bxx2004.pandalib.putil.PDownLoad;
import net.bxx2004.pandalib.putil.PPlugin;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.io.File;
import java.util.List;

public class DownLoadPluginCommand extends PSubcommands {
    @Override
    public String getName() {
        return "download <Name>";
    }

    @Override
    public String getDescription() {
        return "从仓库下载一个插件";
    }

    @Override
    public boolean performCommand(CommandSender commandSender, String[] strings) {
        if (strings.length >= 0){
            if (strings[0].equalsIgnoreCase("download")){
                Player player = (Player) commandSender;
                if (player.hasPermission("cp.use")){
                    if (strings.length < 2 && strings[0].equalsIgnoreCase("download")){
                        PMessage.to(player,"&c请输入参数...");
                    }else {
                        if (CloudPlugin.plugins.keySet().contains(strings[1])){
                            PMessage.to(player,"&c这个插件已存在...");
                            return false;
                        }else {
                            PMessage.to(player,"&7下载任务已经执行,请查看是否成功...");
                            download(0,strings[1]);
                        }
                    }
                }else {
                    PMessage.to(player, "&c你没有权限...");
                }
            }
        }
        return false;
    }
    public void download(int i ,String pluginName){
        String re =  PPlugin.getPlugin("CloudPlugin").getConfig().getStringList("repository").get(i);
        try {
            PDownLoad downLoad = new PDownLoad(re +"/" + pluginName + ".jar","plugins/" + pluginName + ".jar",PPlugin.getPlugin("CloudPlugin"));
            downLoad.start();
        }catch (Exception e){
            download(i + 1,pluginName);
        }
    }
}
